package com.college_career_system.utils;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.college_career_system.pojo.dto.UsersLoginDTO;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author: Empty
 * @Date: 2022-12-05-21:57
 * @Description:
 */
public class TokenUtil {

    /**
     * 生成Token
     * @param usersLoginDTO
     * @return
     */
    public static String getToken(UsersLoginDTO usersLoginDTO){
        return JWT.create().withAudience(usersLoginDTO.getAccount()).sign(Algorithm.HMAC256(Constant.SECRET));
    }


    /**
     * 从token得到账号id
     * @param request
     * @return
     */
    public static String getId(HttpServletRequest request){
        return JWT.decode(request.getHeader("token")).getAudience().get(0);
    }
}
